package com.example.portdemo.viewModel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.portdemo.date.model.NewsEntity;
import com.example.portdemo.service.NewsRepository;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NewsViewModel extends ViewModel {
    private NewsRepository newsRepository;
    private MutableLiveData<List<NewsEntity>> newsList;

    public NewsViewModel(NewsRepository newsRepository) {
        this.newsRepository = newsRepository;
        this.newsList = new MutableLiveData<>();
    }

    public LiveData<List<NewsEntity>> getNewsList(Callback<List<NewsEntity>> callback) {
        return newsList;
    }

    public void loadNewsList() {
        if (newsList.getValue() == null || newsList.getValue().isEmpty()) {
            newsRepository.getNewsList(new Callback<List<NewsEntity>>() {
                @Override
                public void onResponse(Call<List<NewsEntity>> call, Response<List<NewsEntity>> response) {

                }

                @Override
                public void onFailure(Call<List<NewsEntity>> call, Throwable throwable) {

                }
            });
        }
    }
}